package org.jmresler.sakila.batch.config;

import lombok.extern.slf4j.Slf4j;
import org.jmresler.sakila.batch.services.ActorInfoService;
import org.jmresler.sakila.batch.utils.ApplicationListener;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.*;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;

import java.beans.BeanProperty;

@Slf4j
@Configuration
public class BatchConfiguration {

    @Bean
    public Job job(
            final JobRepository repository,
            final Step step,
            final ApplicationListener applicationListener
    ) {
        return new JobBuilder("job", repository)
                // for the purposes of this demo, we don't care about duplicate runs
                // of the batch.
                .incrementer(new RunIdIncrementer())
                .listener(applicationListener)
                .start(step)
                .build();
    }

    @Bean
    public Step step(
            final JobRepository repository,
            final PlatformTransactionManager transactionManager,
            final ApplicationListener applicationListener,
            final ItemReader<?> itemReader,
            final ItemProcessor<?, ?> itemProcessor,
            final ItemWriter<?> itemWriter
    ) {
        return new StepBuilder("step", repository)
                .listener(applicationListener)
                .tasklet((contribution, chunkContext) -> RepeatStatus.FINISHED, transactionManager).build();
    }

    @Bean
    public ItemReader<?> itemReader() {
        return (ItemReader<Object>) () -> null;
    }

    @Bean
    public ItemProcessor<?, ?> itemProcessor() {
        return (ItemProcessor<Object, Object>) item -> null;
    }

    @Bean
    public ItemWriter<?> itemWriter() {
        return (ItemWriter<Object>) chunk -> {};
    }

    @Bean
    public ApplicationListener applicationListener() {
        return new ApplicationListener();
    }
}
